package com.jingan.nursinghome.regulatory.dialog2;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.MainActivity;
import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.view.CircleView;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.triangleView2;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/25.
 */

public class XuantiMenuDialog2 extends BaseDialog {

    public XuantiMenuDialog2(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_richang2_xuanti2, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        initUI();
        adapterUI();
    }

    private static XuantiMenuDialog2 xuantiDialog;
    static String mTitile;
    static List<Topic> mTopicList = new ArrayList<>();

    public static XuantiMenuDialog2 getInstance(BaseActivity baseActivity, String titile, List<Topic> topicList) {
        mTitile = titile;
        mTopicList = topicList;
        if (isFinishing(xuantiDialog)) {
            xuantiDialog = new XuantiMenuDialog2(baseActivity);
        }
        return xuantiDialog;
    }

    MyActionBar myActionBar;
    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    TextView appraise;

    private void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "选题");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                if (itemPosition == 0) {
                    outRect.top = ProportionUtils.getInstance().adapterInt(25);
                }
            }
        });

        appraise = (TextView) findViewById(R.id.appraise);
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //选题完成

                RichangDialog2.richangDialog2.listener();//数据更新

                dismiss();
                XuantiMenuDialog.xuantiDialog.dismiss();
                XuantiMenuDialog3Little.xuantiDialog.dismiss();
            }
        });

    }

    class MyAdapter extends RecyclerView.Adapter<XuantiMenuDialog2.MyAdapter.MyViewHoldView> {
        String category;//类别
        String rid;//题目大类

        public MyAdapter(String category, String rid) {
            this.category = category;
            this.rid = rid;
        }

        class MyViewHoldView extends RecyclerView.ViewHolder {
            TextView info;
            CircleView mycircle;

            public MyViewHoldView(View itemView) {
                super(itemView);
                ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout));
                info = (TextView) itemView.findViewById(R.id.info);
                mycircle = (CircleView) itemView.findViewById(R.id.mycircle);
                ProportionUtils.getInstance().adapterTextView(info);
                ProportionUtils.getInstance().adapterView(itemView.findViewById(R.id.line));
            }
        }

        @Override
        public MyViewHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_xuanti2, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
            return new MyViewHoldView(view);
        }

        @Override
        public void onBindViewHolder(final MyViewHoldView holder, final int position) {
            holder.info.setTextColor(Color.parseColor("#444444"));
            String s = MicrometerTable.getInstance().getcaceRichang();
            if (s != null) {
                String c = mTopicList.get(position).getCount();
                String ss[] = s.split(",");
                for (int i = 0; i < ss.length; i++) {
                    if (ss[i].trim().equals(c.trim())) {
                        holder.info.setTextColor(Color.parseColor("#888888"));
                    }
                }
            }

            holder.info.setText((position + 1) + "." + mTopicList.get(position).getQuestion());
            if (MicrometerTable.getInstance().panduan(mTopicList.get(position).getCount())) {
                holder.mycircle.setState(true);
            } else {
                holder.mycircle.setState(false);
            }

            holder.mycircle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean b = false;
                    if (!holder.mycircle.isState()) {

                        String s = MicrometerTable.getInstance().getcaceRichang();
                        if (s != null) {
                            String c = mTopicList.get(position).getCount();
                            String ss[] = s.split(",");
                            for (int i = 0; i < ss.length; i++) {
                                if (ss[i].trim().equals(c.trim())) {
                                    b = true;
                                    AlertDialog.getInstance(baseActivity, false, false).setTitle("提示").setMession("上次已经勾选过该题!\n是否继续勾选？").setPositive("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            holder.mycircle.setState(!holder.mycircle.isState());
                                            if (holder.mycircle.isState()) {
                                                MicrometerTable.getInstance().addRiChange(mTopicList.get(position).getCount());
                                            } else {
                                                MicrometerTable.getInstance().jianRiChange(mTopicList.get(position).getCount());
                                            }
                                            AlertDialog.dismisss();
                                        }
                                    });
                                }
                            }
                        }

                    }
                    if (!b) {
                        holder.mycircle.setState(!holder.mycircle.isState());
                        if (holder.mycircle.isState()) {
                            MicrometerTable.getInstance().addRiChange(mTopicList.get(position).getCount());
                        } else {
                            MicrometerTable.getInstance().jianRiChange(mTopicList.get(position).getCount());
                        }
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mTopicList.size();
        }
    }

    private void adapterUI() {
//        ProportionUtils.getInstance().adapterView(findViewById(R.id.dialog_richang2_constraintLayout));
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_richang2_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(appraise);
    }

    @Override
    protected void listener() {
        myActionBar.setLeftBimapAndTitle(null, "选题-" + mTitile);
        recyclerView.setAdapter(new MyAdapter(mTitile, CreateMicrometerTableDailog2.rid));
    }

    @Override
    protected void recycleView() {

    }
}
